//
//  CLsameViewController.m
//  I-want-go
//
//  Created by dllo on 15/11/3.
//  Copyright (c) 2015年 lanou3g. All rights reserved.
//

#import "CLsameViewController.h"
#import "CLsameCollectionCell.h"
#import "BaseModel.h"
#import "CLsame.h"
#import "CLtool.h"
#import "CLscreen.h"
#import "CLinterViewController.h"
@interface CLsameViewController ()<UICollectionViewDataSource, UICollectionViewDelegate>
@property(nonatomic, retain)UICollectionView *myCollectionView;
@property(nonatomic, retain)NSMutableArray *arr;
@property(nonatomic, assign)NSInteger page;
@end

@implementation CLsameViewController
- (void)dealloc {
    [_arr release];
    [_myCollectionView release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor purpleColor];
    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.itemSize = CGSizeMake(170 * SWIDTH, 200 * SHEIGHT);
    
    self.myCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, 375 * SWIDTH, 601 * SHEIGHT) collectionViewLayout:flowLayout];
    [self.view addSubview:self.myCollectionView];
    
    [_myCollectionView release];
    self.myCollectionView.backgroundColor = [UIColor whiteColor];
    [self.myCollectionView registerClass:[CLsameCollectionCell class] forCellWithReuseIdentifier:@"reuse"];
    
    self.myCollectionView.dataSource = self;
    self.myCollectionView.delegate = self;
    
    
    self.myCollectionView.footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(footerAction)];
    
    [self createData];

}

- (void)footerAction {
    
    self.page = 0;
    self.page++;
    NSString *strid = [NSString stringWithFormat:@"brandId=%@&fromType=-1&imageHD=2&key=8ba3cfaa4de658532d805e3a416d49b8&page=%ld&t=1446541744776&userid=705811&version=3.5.4", self.sameId, self.page];
    NSString *str = @"http://api.xiangqu.com/search/list";
    NSURL *url = [NSURL URLWithString:str];
    NSString *bodyStr = strid;
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    [request setHTTPMethod:@"POST"];
    NSData *bodyData = [bodyStr dataUsingEncoding:NSUTF8StringEncoding];
    [request setHTTPBody:bodyData];
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
        
        NSMutableArray *arArr = [NSMutableArray array];
        arArr = [CLsame baseModelByArr:dic[@"data"][@"records"]];
        for (CLsame *same in arArr) {
            [self.arr addObject:same];
        }
        [self.arr removeLastObject];
        [self.myCollectionView.footer endRefreshing];
        [self.myCollectionView reloadData];
        
    }];
    
}



- (void)createData {
    
    NSString *strid = [NSString stringWithFormat:@"brandId=%@&fromType=-1&imageHD=2&key=8ba3cfaa4de658532d805e3a416d49b8&page=0&t=1446541744776&userid=705811&version=3.5.4", self.sameId];
    NSString *str = @"http://api.xiangqu.com/search/list";
    NSURL *url = [NSURL URLWithString:str];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    [request setHTTPMethod:@"POST"];
    NSString *bodyStr = strid;
    NSData *bodyData = [bodyStr dataUsingEncoding:NSUTF8StringEncoding];
    [request setHTTPBody:bodyData];
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
        
        self.arr = [NSMutableArray array];
        self.arr = [CLsame baseModelByArr:dic[@"data"][@"records"]];
        
        [self.myCollectionView reloadData];
    }];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.arr.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    CLsameCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"reuse" forIndexPath:indexPath];
    
    CLsame *same = self.arr[indexPath.row];
    
    [cell.myImage sd_setImageWithURL:[NSURL URLWithString:same.image]];
    cell.proLabel.text = same.brand;
    
    cell.addLabel.text = same.productDescription;
    
    
    
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    CLinterViewController *intVC = [[CLinterViewController alloc] init];
    [self.navigationController pushViewController:intVC animated:YES];
    
    CLsame *same = self.arr[indexPath.row];
    
    intVC.interId = same.productId;
    
    
}

- (void)viewWillAppear:(BOOL)animated {
    
    self.tabBarController.tabBar.hidden = YES;
    
}

- (void)viewWillDisappear:(BOOL)animated {
    self.tabBarController.tabBar.hidden = NO;
}









- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
